package com.wzs.exceltotxt;

import com.wzs.exceltotxt.model.Excel;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Controller {
    @FXML
    private Button chooseFileButton;

    @FXML
    private Label filePathLabel;

    private String filePath;

    @FXML
    protected void handleChooseFile() {
        FileChooser fileChooser = new FileChooser();
        //设置文件选择对话框的初始目录、文件过滤
        fileChooser.setTitle("选择文件");
        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("文件(*.xlsx)", "*.xlsx");
        fileChooser.getExtensionFilters().add(extFilter);

        //打开对话框并获取用户选择的文件
        Stage stage = (Stage) chooseFileButton.getScene().getWindow();
        File selectedFile = fileChooser.showOpenDialog(stage);

        if (selectedFile != null) {
            String absolutePath = selectedFile.getAbsolutePath();
            filePath = absolutePath;
            filePathLabel.setText("选定的文件：" + absolutePath);
        }
    }

    @FXML
    protected void handleCreateTxt() {
        if (filePath == null) {
            return;
        }
        List<Excel> excelList = new ArrayList<>();
        try {
            FileInputStream fis = new FileInputStream(filePath);
            XSSFWorkbook workbook = new XSSFWorkbook(fis);
            Sheet sheet = workbook.getSheetAt(0);

            Excel excel;
            for (Row row : sheet) {
                if (row.getRowNum() == 0) {
                    continue;
                }
                excel = new Excel();
                excel.setTurnout(row.getCell(0).getStringCellValue());
                excel.setPointSection(row.getCell(1).getStringCellValue());
                excelList.add(excel);
            }
            workbook.close();
            fis.close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath.replace(".xlsx", ".txt")))) {
            String[] split;
            for (Excel excel : excelList) {
                writer.newLine();
                writer.write(Application.map.get(excel.getTurnout().toUpperCase()) + ",");
                writer.newLine();
                split = excel.getPointSection().replace(";", ",").split(",");
                writer.write(split.length + ",");
                writer.newLine();
                for (String s : split) {
                    writer.write(Application.map.get(s.trim().toUpperCase()) + ",");
                }
                writer.newLine();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("提示");
        alert.setHeaderText(null);
        alert.setContentText("生成txt文件成功");
        alert.showAndWait();
    }
}